package com.yycx.module.bus.provider.controller.hy;

import cn.hutool.core.map.MapUtil;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.provider.service.hy.HyRemoteOrderService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * description: 订单2凭证
 * date: 2023年-12月-04日 13:14
 * author: ldd
 */
@RestController
@RequestMapping("/hy/order/voucher/")
public class HyOrderVoucherController {

    @Autowired
    private HyRemoteOrderService hyRemoteOrderService;

    @Autowired
    private SysCompanyService companyService;

    @ApiOperation(value = "订单凭证-分页列表", notes = "订单凭证分页列表")
    @GetMapping(value = "page")
    public ResultBody pageList(@RequestParam(required = false) Map params) {
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        String dbName = company.getCompanyErpInstance();
        params.put("dbName", dbName);
        params.put("companyCode", company.getCompanyCode());
        String beginDate= MapUtil.getStr(params,"beginDate", DateUtils.format(DateUtils.getFirstDayOfMonth()));
        String endDate= MapUtil.getStr(params,"endDate", DateUtils.format(DateUtils.getLastDayOfMonth()));
        params.put("beginDate", beginDate);
        params.put("endDate", endDate);
        return hyRemoteOrderService.hyOrderVoucherList(params);
    }


    @ApiOperation(value = "订单凭证-详情", notes = "订单凭证详情")
    @GetMapping(value = "get")
    public ResultBody get(@RequestParam Map params) {
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        String dbName = company.getCompanyErpInstance();
        params.put("dbName", dbName);
        params.put("companyCode", company.getCompanyCode());
        return ResultBody.ok(hyRemoteOrderService.getHyOrderVoucherByOrderNo(params));
    }

    @ApiOperation(value = "海鹰erp订单-生成单据凭证", notes = "海鹰erp订单-生成单据凭证")
    @PostMapping(value = "createVoucher")
    public ResultBody createVoucher(@RequestParam(required = false) Map params) {
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        String dbName = company.getCompanyErpInstance();
        params.put("dbName", dbName);
        params.put("companyCode", company.getCompanyCode());
        params.put("companyId", company.getCompanyId());
        return hyRemoteOrderService.createVoucher(params);
    }

    @ApiOperation(value = "海鹰erp订单-合并单据生成凭证", notes = "海鹰erp订单-合并单据生成凭证")
    @PostMapping(value = "mergeVoucher")
    public ResultBody mergeVoucher(@RequestParam(required = false) Map params) {
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        String dbName = company.getCompanyErpInstance();
        params.put("dbName", dbName);
        params.put("companyCode", company.getCompanyCode());
        params.put("companyId", company.getCompanyId());
        return hyRemoteOrderService.mergeVoucher(params);
    }

    @ApiOperation(value = "海鹰erp订单-合并生成当月单据凭证", notes = "海鹰erp订单-合并生成当月单据凭证")
    @PostMapping(value = "monthVoucher")
    public ResultBody monthVoucher(@RequestParam(required = false) Map params) {
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        String dbName = company.getCompanyErpInstance();
        params.put("dbName", dbName);
        params.put("companyCode", company.getCompanyCode());
        params.put("companyId", company.getCompanyId());
        return hyRemoteOrderService.monthVoucher(params);
    }
}
